﻿namespace ManagerClinic.KhamBenh
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Ultilites.HtmlEditor.Buttons;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Ultilities.Ultility;
    using ManagerClinic.Modules;
    using System.Linq;
    using System.Configuration;

    public partial class frmPhieuKhamBenh : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler _savePhieuKhamBenh = null;

        HoSoKhamBenhNgoaiTruEntity hoSoKhamBenhNgoaiTruEntity = null;
        GridCheckMarksSelection gridCheckMarksSelection = null;
        PhieuKhamBenhEntity phieuKhamBenhEntity = null;

        HoSoKhamBenhNgoaiTruBusiness hoSoKhamBenhNgoaiTruBusiness = new HoSoKhamBenhNgoaiTruBusiness();
        ThuPhiDichVuBusiness thuPhiDichVuBusiness = new ThuPhiDichVuBusiness();
        DmKhoaBusiness dmKhoaBusiness = new DmKhoaBusiness();
        DmPhongKhamBusiness dmPhongKhamBusiness = new DmPhongKhamBusiness();
        PhieuKhamBenhBusiness phieuKhamBenhBusiness = new PhieuKhamBenhBusiness();
        PhieuKhamBenhChiTietBusiness phieuKhamBenhChiTietBusiness = new PhieuKhamBenhChiTietBusiness();
        DmDichVuChiTietBusiness dmDichVuChiTietBusiness = new DmDichVuChiTietBusiness();

        string maKhamBenh = string.Empty, maDichVu = string.Empty;
        bool isInserted = false;
        string sMaKhoaKham =  ConfigurationManager.AppSettings["MaKhoaKham"].ToString();
        string sMaPhongKham = ConfigurationManager.AppSettings["MaPhongKham"].ToString();

        public frmPhieuKhamBenh(HoSoKhamBenhNgoaiTruEntity obj, string _maDichVu)
        {
            InitializeComponent();
            PredefinedButtonSets.SetupDefaultButtons(this.htmlKetQua);

            hoSoKhamBenhNgoaiTruEntity = obj;
            gridCheckMarksSelection = new GridCheckMarksSelection(gvDichVu);
            this.ucThongTinHanhChinh.LoadThongTinHanhChinh(hoSoKhamBenhNgoaiTruEntity.MaBenhNhan);

            maKhamBenh = ConstantManagerClinic.setCodeManagerClinic("KB");
            LoadDmKhoaKham();
            LoadDmPhongKham();

            maDichVu = _maDichVu;
            LoadDmDichVuChiTiet(maDichVu);
        }

        public frmPhieuKhamBenh(string _maKhamBenh)
        {
            InitializeComponent();
            PredefinedButtonSets.SetupDefaultButtons(this.htmlKetQua);

            maKhamBenh = _maKhamBenh;
            isInserted = true;
            phieuKhamBenhEntity = phieuKhamBenhBusiness.GetPhieuKhamBenh(maKhamBenh);
            //check trang thai phieu kham benh
            // neu trang thai phieu la tao moi thi duoc chi dinh chi tieu
            // con ngoai ra chi dc kham khong duoc chi dinh tiep
            if (phieuKhamBenhEntity.TrangThaiPhieu != (int)ConstantManagerClinic.TrangThaiKhamBenh.TaoMoi)
            {
                btnLuu.Enabled = false;
            }

            hoSoKhamBenhNgoaiTruEntity = hoSoKhamBenhNgoaiTruBusiness.GetHoSoKhamBenhNgoaiTru(phieuKhamBenhEntity.MaHoSo);
            gridCheckMarksSelection = new GridCheckMarksSelection(gvDichVu);
            this.ucThongTinHanhChinh.LoadThongTinHanhChinh(hoSoKhamBenhNgoaiTruEntity.MaBenhNhan);

            LoadDmKhoaKham();
            LoadDmPhongKham();
            maDichVu = phieuKhamBenhEntity.MaDichVu;
            LoadDmDichVuChiTiet(maDichVu);
        }

        public IList<DmDichVuChiTietEntity> lstDmDichVuChiTietEntity { get; set; }
        /// <summary>
        /// Load datagrid DmDichVuChiTiet
        /// </summary>
        /// <param name="maDichVu">MaDichVu</param>
        public void LoadDmDichVuChiTiet(string maDichVu)
        {
            lstDmDichVuChiTietEntity = dmDichVuChiTietBusiness.GetDmDichVuChiTietByDichVu(maDichVu);
            grdDichVu.DataSource = lstDmDichVuChiTietEntity;
        }

        /// <summary>
        /// Get, set value of DichVuChiTietEntity is checked
        /// </summary>
        public IList<DmDichVuChiTietEntity> DmDichVuChiTietEntityIsChecked { get; set; }

        /// <summary>
        /// Isvalid grid DichVuChiTiet 
        /// </summary>
        /// <returns>All DmDichVuChitietEntity has choosed</returns>
        public void IsValidChooseDichVuChiTiet()
        {
            if (gridCheckMarksSelection.selection.Count <= 0)
            {
                MessageBox.Show("Bạn chưa chọn dịch vụ khám chữa bệnh!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }
            DmDichVuChiTietEntityIsChecked = new List<DmDichVuChiTietEntity>();
            foreach (var o in gridCheckMarksSelection.selection)
            {
                DmDichVuChiTietEntityIsChecked.Add((DmDichVuChiTietEntity)o);
            }
        }

        /// <summary>
        /// Set value has choosed on the grid
        /// </summary>
        /// <param name="maPhieuDichVu">maPhieuDichVu: Ma Phieu Sieu Am, Ma Phieu XQuang ...</param>
        /// <param name="maDichVuKhamChuaBenh">maDichVuKhamChuaBenh: Kham Benh, Sieu Am</param>
        public void setDmDichVuChiTietEntityIsChecked(string maPhieuDichVu, string maDichVuKhamChuaBenh)
        {
            var lstThuPhiEntity = thuPhiDichVuBusiness.GetThuPhiDichVuListByMaPhieuDichVu_MaDichVu(maPhieuDichVu, maDichVuKhamChuaBenh);
            foreach (ThuPhiDichVuEntity thuphiEntity in lstThuPhiEntity)
            {
                for (int i = 0; i < gvDichVu.RowCount; i++)
                {
                    var dmDichVuChiTietEntity = (DmDichVuChiTietEntity)gvDichVu.GetRow(i);
                    if (dmDichVuChiTietEntity.MaChiTieu.Trim() == thuphiEntity.MaChiTieu.Trim())
                    {
                        gridCheckMarksSelection.SelectRow(i, true);
                        continue;
                    }
                }
            }
        }

        /// <summary>
        /// Set datasource of lkDmKhoaKham
        /// </summary>
        public void LoadDmKhoaKham()
        {
            var lstDmKhoaEntity = dmKhoaBusiness.GetDmKhoaList();
            lstDmKhoaEntity.Insert(0, new DmKhoaEntity() { MaKhoa = ConstantManagerClinic.sValueMember, TenKhoa = ConstantManagerClinic.sDisplayMember });
            lkKhoaKhamBenh.Properties.DataSource = lstDmKhoaEntity;
            lkKhoaKhamBenh.Properties.DisplayMember = "TenKhoa";
            lkKhoaKhamBenh.Properties.ValueMember = "MaKhoa";
            lkKhoaKhamBenh.EditValue = sMaKhoaKham;
        }

        /// <summary>
        /// Set datasource of lkPhongKham
        /// </summary>
        public void LoadDmPhongKham()
        {
            var lstDmPhongKhamEntity = dmPhongKhamBusiness.GetDmPhongKhamList();
            lstDmPhongKhamEntity = lstDmPhongKhamEntity.Where(n => n.MaKhoa == lkKhoaKhamBenh.EditValue.ToString()).ToList();
            lstDmPhongKhamEntity.Insert(0, new DmPhongKhamEntity() { MaPhongKham = ConstantManagerClinic.sValueMember, TenPhongKham = ConstantManagerClinic.sDisplayMember });
            //set datasource lkPhongKham
            lkPhongKhamBenh.Properties.DataSource = lstDmPhongKhamEntity;
            lkPhongKhamBenh.Properties.DisplayMember = "TenPhongKham";
            lkPhongKhamBenh.Properties.ValueMember = "MaPhongKham";
            lkPhongKhamBenh.EditValue = sMaPhongKham;
        }

        void LoadDataPhieuKhamBenh()
        {
            phieuKhamBenhEntity = phieuKhamBenhBusiness.GetPhieuKhamBenh(maKhamBenh);
            if (phieuKhamBenhEntity == null) return;

            setDmDichVuChiTietEntityIsChecked(maKhamBenh, maDichVu);
            lkKhoaKhamBenh.EditValue = phieuKhamBenhEntity.MaKhoa;
            lkPhongKhamBenh.EditValue = phieuKhamBenhEntity.MaPhongKham;
            htmlKetQua.Html = phieuKhamBenhEntity.KetLuan;
        }

        private void frmPhieuKhamBenh_Load(object sender, EventArgs e)
        {
            LoadDataPhieuKhamBenh();
        }

        /// <summary>
        /// The method support to get YeuCau
        /// </summary>
        /// <returns></returns>
        string getYeuCauKham()
        {
            var lstYeuCauEntity = DmDichVuChiTietEntityIsChecked.Select(n => n.TenChiTieu).ToList();
            return string.Join(",", lstYeuCauEntity.ToArray());
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (Validation.ValidLookUpEdit(lkKhoaKhamBenh, "khoa khám bệnh") == false) return;
            if (Validation.ValidLookUpEdit(lkPhongKhamBenh, "phòng khám") == false) return;
            this.IsValidChooseDichVuChiTiet();

            if (isInserted == false)
            {
                phieuKhamBenhEntity = new PhieuKhamBenhEntity()
                {
                    MaKhamBenh = maKhamBenh,
                    Stt = 0,
                    MaHoSo = hoSoKhamBenhNgoaiTruEntity.MaHoSo,
                    MaDichVu = maDichVu,
                    SoVaoVien = string.Empty,
                    NgayVaoKham = hoSoKhamBenhNgoaiTruEntity.NgayVaoKham,
                    LyDoKham = getYeuCauKham(),
                    KetLuan = htmlKetQua.Html,
                    MaBacSy = Authenticate.MaBacSy,
                    TrangThaiPhieu = (int)ConstantManagerClinic.TrangThaiKhamBenh.TaoMoi,
                    MaKhoa = lkKhoaKhamBenh.EditValue.ToString(),
                    MaPhongKham = lkPhongKhamBenh.EditValue.ToString(),
                    TrangThaiCho = Convert.ToBoolean(ConstantManagerClinic.TrangThaiChoDichVuKhamChuaBenh.ChuaThucHien)
                };
                isInserted = phieuKhamBenhBusiness.InsertPhieuKhamBenh(phieuKhamBenhEntity);
            }
            else
            {
                phieuKhamBenhEntity = phieuKhamBenhBusiness.GetPhieuKhamBenh(maKhamBenh);
                phieuKhamBenhEntity.LyDoKham = getYeuCauKham();
                phieuKhamBenhEntity.KetLuan = htmlKetQua.Html;
                phieuKhamBenhEntity.MaBacSy = Authenticate.MaBacSy;
                phieuKhamBenhEntity.TrangThaiPhieu = (int)ConstantManagerClinic.TrangThaiKhamBenh.TaoMoi;
                phieuKhamBenhEntity.MaKhoa = lkKhoaKhamBenh.EditValue.ToString();
                phieuKhamBenhEntity.MaPhongKham = lkPhongKhamBenh.EditValue.ToString();
                phieuKhamBenhEntity.TrangThaiCho = Convert.ToBoolean(ConstantManagerClinic.TrangThaiChoDichVuKhamChuaBenh.ChuaThucHien);
                phieuKhamBenhBusiness.EditPhieuKhamBenh(phieuKhamBenhEntity);
            }

            phieuKhamBenhChiTietBusiness.DeletePhieuKhamBenhChiTietByMaKhamBenh(maKhamBenh);
            thuPhiDichVuBusiness.DeleteThuPhiDichVuByMaPhieuDichVu(maKhamBenh);

            foreach (var dv in DmDichVuChiTietEntityIsChecked)
            {
                var thuPhiEntity = new ThuPhiDichVuEntity()
                {
                    MaPhieuDichVu = maKhamBenh,
                    MaHoSo = hoSoKhamBenhNgoaiTruEntity.MaHoSo,
                    NgayDenKham = hoSoKhamBenhNgoaiTruEntity.NgayVaoKham,
                    MaDichVu = maDichVu,
                    MaChiTieu = dv.MaChiTieu,
                    TongTien = dv.DonGia,
                    DaDongTien = (int)ConstantManagerClinic.TrangThaiDongTien.ChuaDongTien,
                    MaNguoiDung = Authenticate.UserName,
                    MaKhoaKham = lkKhoaKhamBenh.EditValue.ToString(),
                    MaPhongKham = lkPhongKhamBenh.EditValue.ToString(),
                    MaKhoaCanLamSang = string.Empty,
                    MaPhongCanLamSang = string.Empty,
                    DaThucHien = Convert.ToBoolean(ConstantManagerClinic.TrangThaiThucHienDichVuKhamChuaBenh.ChuaThucHien)
                };
                thuPhiDichVuBusiness.InsertThuPhiDichVu(thuPhiEntity);

                var phieuKhamBenhChiTietEntity = new PhieuKhamBenhChiTietEntity()
                {
                    MaKhamBenh = maKhamBenh,
                    MaChiTieu = dv.MaChiTieu,
                    DonGia = dv.DonGia,
                    DaThuTien = (int)ConstantManagerClinic.TrangThaiThucHienDichVuKhamChuaBenh.ChuaThucHien
                };
                phieuKhamBenhChiTietBusiness.InsertPhieuKhamBenhChiTiet(phieuKhamBenhChiTietEntity);
            }

            if (_savePhieuKhamBenh != null)
            {
                _savePhieuKhamBenh(sender, e);
            }
        }

        private void btnLuuKetQua_Click(object sender, EventArgs e)
        {
            phieuKhamBenhEntity = phieuKhamBenhBusiness.GetPhieuKhamBenh(maKhamBenh);
            phieuKhamBenhEntity.KetLuan = htmlKetQua.Html;
            phieuKhamBenhEntity.MaBacSy = Authenticate.MaBacSy;
            phieuKhamBenhBusiness.EditPhieuKhamBenh(phieuKhamBenhEntity);
            MessageBox.Show("Lưu kết quả thành công", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
        }

        private void lkKhoaKhamBenh_EditValueChanged(object sender, EventArgs e)
        {
            LoadDmPhongKham();
        }
    }
}